﻿#include "slabel.h"
#include <QPainter>
#include <QTimer>
#include <QFontMetrics>
#include <QTimerEvent>
#include <QDebug>

const QString strSpace("   ");

SLabel::SLabel(QWidget *parent) : QLabel(parent)
{
//    setStyleSheet(QString("background-color: rgb(0x3F,0x4F,0x75);font: 12pt \"Arial\";color: rgb(255, 0, 0);"));

    m_pImage = NULL;
    m_nFontSize = 12;

    m_nActImgWidth = rect().width();
    m_nActImgHeight = rect().height();

    upateLabel();
}

void SLabel::setText(const QString & txt)
{
    QLabel::setText(txt);

    m_Obj_Type = Obj_Text;
    upateLabel();
}

void SLabel::setImage(const QString &strImgFile)
{
    if(m_pImage)
    {
        delete m_pImage;
        m_pImage = NULL;
    }

    m_Obj_Type = Obj_Image;
    m_strImgFile = strImgFile;
    m_pImage = new QImage(strImgFile);

    int nImgWidth  = m_pImage->width();
    int nImgHeight = m_pImage->height();

    int nWidth  = rect().width();
    int nHeight = rect().height();

    //显示比例图像
    int nScale_Width = (1.0 * nImgWidth / nWidth) * 100;
    int nScale_Height = (1.0 * nImgHeight / nHeight) * 100;

    if(nScale_Width > nScale_Height) //宽度比例大，按高度比例来
    {
        m_nImgScale = nScale_Width;
    }
    else //
    {
        m_nImgScale = nScale_Height;
    }

    m_nActImgWidth = (int)(100.0 * m_pImage->width() / m_nImgScale);
    m_nActImgHeight = (int)(100.0 * m_pImage->height() / m_nImgScale);

    if(m_nActImgWidth > rect().width())
        m_nActImgWidth = rect().width();

    if(m_nActImgHeight > rect().height())
        m_nActImgHeight = rect().height();

    repaint();
}

void SLabel::setFontSize(int nFontSize)
{
    m_nFontSize = nFontSize;
    repaint();
}

void SLabel::setAlign(enumAlign_Mode nAlign_Mode)
{
    m_nAlign = nAlign_Mode;
    repaint();
}

void SLabel::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    QRect rc = rect();

    if(!m_pImage)
        return;

    if(m_nAlign == Align_Left)
    {
        //左对齐
        QRectF target(0, (rc.height() - m_nActImgHeight) / 2, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
        QRectF source(0, 0, m_pImage->width(), m_pImage->height()); //建立源矩形，用来框定源图像文件中要显示的部分
        painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
    }
    else if(m_nAlign == Align_Right)
    {
        QRectF target(rc.width() - m_nActImgWidth, (rc.height() - m_nActImgHeight) / 2, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
        QRectF source(0, 0, m_pImage->width(), m_pImage->height()); //建立源矩形，用来框定源图像文件中要显示的部分
        painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
    }
    else
    {
        //居中
        QRectF target((rc.width() - m_nActImgWidth) / 2, (rc.height() - m_nActImgHeight) / 2, m_nActImgWidth, m_nActImgHeight); //建立目标矩形
        QRectF source(0, 0, m_pImage->width(), m_pImage->height()); //建立源矩形，用来框定源图像文件中要显示的部分
        painter.drawImage(target, *m_pImage, source); //将源图像文件中(0.0, 0.0, 70.0, 40.0)位置的图像画到painter device中的
    }
}

void SLabel::resizeEvent(QResizeEvent *e)
{
    QLabel::resizeEvent(e);

    upateLabel();
}

void SLabel::upateLabel()
{
    if(m_Obj_Type == Obj_Image)
    {
        if(m_pImage)
        {
            delete m_pImage;
            m_pImage = NULL;
        }

        m_pImage = new QImage(m_strImgFile);

        int nImgWidth  = m_pImage->width();
        int nImgHeight = m_pImage->height();

        int nWidth  = rect().width();
        int nHeight = rect().height();

        //显示比例图像
        int nScale_Width = (1.0 * nImgWidth / nWidth) * 100;
        int nScale_Height = (1.0 * nImgHeight / nHeight) * 100;

        if(nScale_Width > nScale_Height) //宽度比例大，按高度比例来
        {
            m_nImgScale = nScale_Width;
        }
        else //
        {
            m_nImgScale = nScale_Height;
        }

        m_nActImgWidth = (int)(100.0 * m_pImage->width() / m_nImgScale);
        m_nActImgHeight = (int)(100.0 * m_pImage->height() / m_nImgScale);

        if(m_nActImgWidth > rect().width())
            m_nActImgWidth = rect().width();

        if(m_nActImgHeight > rect().height())
            m_nActImgHeight = rect().height();
    }
    else
    {
        QRect rc = rect();

        m_ft = font();
        m_ft.setPointSize(m_nFontSize);

        QFontMetrics fm(m_ft);

        QString strText = text();

        if(strText.length() <= 0)
        {
            m_nActImgWidth  = 0;
            m_nActImgHeight = 0;

            if(m_pImage)
            {
                delete m_pImage;
                m_pImage = NULL;
            }
        }
        else
        {
            m_nActImgWidth  = fm.width(strText) + 2;
            m_nActImgHeight = m_nFontSize + 2;

            if(m_pImage)
            {
                delete m_pImage;
                m_pImage = NULL;
            }

            m_pImage = new QImage(m_nActImgWidth, m_nActImgHeight, QImage::Format_RGB32);

            QPainter painterImg(m_pImage);
            m_pImage->fill(QColor(0, 0, 0, 0));

            painterImg.setFont(m_ft);
            painterImg.setPen(QPen(Qt::white));

            painterImg.drawText(1, m_nActImgHeight - 1, strText);
        }
    }

    repaint();
}
